package com.da.auth.controller;

import com.da.common.entity.PageResult;
import com.da.common.enums.ResultEnum;
import com.da.common.exception.ParameterException;
import com.da.common.utils.DailyAimResult;
import com.da.auth.convert.DailyPermissionConverter;
import com.da.auth.entity.DailyPermissions;
import com.da.auth.entity.dto.DailyPermissionsDTO;
import com.da.auth.service.DailyPermissionsService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-01-21
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class DailyPermissionController {

    private final DailyPermissionsService dailyPermissionsService;

    public DailyPermissionController(DailyPermissionsService dailyPermissionsService) {
        this.dailyPermissionsService = dailyPermissionsService;
    }

    @PostMapping("/addPermission")
    @Operation(summary = "新增权限", description = "新增权限")
    public DailyAimResult<Boolean> addPermission(@RequestBody DailyPermissionsDTO dailyPermissionsDTO) {
        Optional.ofNullable(dailyPermissionsDTO)
                .orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "dailyPermissionsDTO为空！"));
        DailyPermissions dailyPermissions = DailyPermissionConverter.INSTANCE.convertToEntity(dailyPermissionsDTO);
        Boolean isAddPermission = dailyPermissionsService.addPermission(dailyPermissions);
        if (isAddPermission) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "新增权限成功", true);
        } else {
            return DailyAimResult.error(ResultEnum.FAIL.getCode(), "新增权限失败");
        }
    }

    @GetMapping("/queryPageByPermission")
    @Operation(summary = "分页查询权限", description = "分页查询权限")
    public DailyAimResult<PageResult<DailyPermissions>> queryPageByPermission(@RequestParam(defaultValue = "1") int pageNum,
                                                                              @RequestParam(defaultValue = "10") int pageSize) {
        return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "查询成功", dailyPermissionsService.queryPageByPermission(pageNum, pageSize));
    }

    @GetMapping("/queryPermissionInfoById/{id}")
    @Operation(summary = "根据id查询权限", description = "根据id查询权限")
    public DailyAimResult<?> queryPermissionInfoById(@PathVariable String id) {
        Optional.ofNullable(id).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "id不能为空！"));
        DailyPermissions dailyPermissions = dailyPermissionsService.queryPermissionInfoById(id);
        return !ObjectUtils.isEmpty(dailyPermissions) ? DailyAimResult.success("查询用户成功", dailyPermissions) : DailyAimResult.error(ResultEnum.SUCCESS.getCode(), "");
    }

    @DeleteMapping("/deletedPermission/{id}")
    @Operation(summary = "删除权限", description = "删除权限")
    public DailyAimResult<?> deletedPermission(@PathVariable String id) {
        Optional.ofNullable(id).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "id不能为空！"));
        Boolean isDeletedPermission = dailyPermissionsService.deletedPermission(id);
        if (isDeletedPermission) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "删除角色成功", true);
        } else {
            return DailyAimResult.error(ResultEnum.FAIL.getCode(), "删除角色失败");
        }
    }


}
